package com.mb.ext.core.dao.impl.seckill;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.NoResultException;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.mb.ext.core.dao.AbstractBaseDAO;
import com.mb.ext.core.dao.seckill.SecKillProductDAO;
import com.mb.ext.core.entity.merchant.MerchantEntity;
import com.mb.ext.core.entity.seckill.SecKillProductEntity;
import com.mb.framework.exception.DAOException;
import com.mb.framework.util.log.LogHelper;

@Repository("secKillProductDAO")
@Qualifier("secKillProductDAO")
public class SecKillProductDAOImpl extends AbstractBaseDAO<SecKillProductEntity> implements SecKillProductDAO {
	private final LogHelper logger = LogHelper.getInstance(this.getClass().getName());

	/**
	 * Initializing Entity.
	 */
	public SecKillProductDAOImpl() {
		super();
		this.setEntityClass(SecKillProductEntity.class);
	}

	@Override
	public void addSecKill(SecKillProductEntity secKillProductEntity) throws DAOException {

		this.save(secKillProductEntity);

	}

	@Override
	public void updateSecKill(SecKillProductEntity secKillProductEntity) throws DAOException {

		this.update(secKillProductEntity);

	}

	@Override
	public void deleteSecKill(SecKillProductEntity secKillProductEntity) throws DAOException {

		this.delete(secKillProductEntity);

	}

	@Override
	public SecKillProductEntity getSecKillByUuid(String uuid) throws DAOException {
		SecKillProductEntity secKillProductEntity = null;
		try {
			secKillProductEntity = (SecKillProductEntity) em.createQuery(
					"select b from SecKillProductEntity b where  b.secKillProductUuid = :UUID and b.isDeleted=:isDeleted",
					SecKillProductEntity.class).setParameter("UUID", uuid).setParameter("isDeleted", Boolean.FALSE)
					.getSingleResult();
		} catch (NoResultException e) {
			logger.info("No record found for product : " + uuid);
		}
		return secKillProductEntity;
	}

	@Override
	public List<SecKillProductEntity> getActiveSecKills() throws DAOException {
		List<SecKillProductEntity> secKillProductEntityList = new ArrayList<SecKillProductEntity>();
		try {
			secKillProductEntityList = em.createQuery(
					"select b from SecKillProductEntity b where b.status = :STATUS and b.isDeleted=:isDeleted order by b.startTime",
					SecKillProductEntity.class)
					.setParameter("STATUS", "1")
					.setParameter("isDeleted", Boolean.FALSE).getResultList();
		} catch (NoResultException e) {
			logger.info("No record found for sec kill");
		}
		return secKillProductEntityList;
	}

	@Override
	public List<SecKillProductEntity> getAllSecKills() throws DAOException {
		List<SecKillProductEntity> secKillProductEntityList = new ArrayList<SecKillProductEntity>();
		try {
			secKillProductEntityList = em.createQuery("select b from SecKillProductEntity b where  b.isDeleted=:isDeleted order by b.startTime",SecKillProductEntity.class).setParameter("isDeleted", Boolean.FALSE).getResultList();
		} catch (NoResultException e) {
			logger.info("No record found for sec kill");
		}
		return secKillProductEntityList;
	}
	
	@Override
	public List<SecKillProductEntity> getSecKillsByMerchant(MerchantEntity merchantEntity) throws DAOException {
		List<SecKillProductEntity> secKillProductEntityList = new ArrayList<SecKillProductEntity>();
		try {
			secKillProductEntityList = em.createQuery("select b from SecKillProductEntity b where b.merchantEntity.merchantUuid = :MERCHANTUUID and b.isDeleted=:isDeleted order by b.startTime",SecKillProductEntity.class)
					.setParameter("MERCHANTUUID", merchantEntity.getMerchantUuid())
					.setParameter("isDeleted", Boolean.FALSE)
					.getResultList();
		} catch (NoResultException e) {
			logger.info("No record found for sec kill");
		}
		return secKillProductEntityList;
	}

	@Override
	public void decrStock(String secKillProductUuid) throws DAOException {
		try {
			String nativeSql = "update sec_kill_product set stock = stock - 1 where sec_kill_product_uuid = '" + secKillProductUuid + "'";
			em.createNativeQuery(nativeSql).executeUpdate();
		} catch (Exception e) {
			throw new DAOException("更新秒杀商品库存失败");
		}
		
		
	}
}
